package com.hzy.fivepalace.admin.service;


import cn.binarywang.wx.miniapp.api.WxMaService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzy.fivepalace.admin.controller.base.BaseController;
import com.hzy.fivepalace.admin.dao.*;
import com.hzy.fivepalace.common.exception.AppException;
import com.hzy.fivepalace.common.type.VipTypeEnums;
import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.common.util.TableDataInfo;
import com.hzy.fivepalace.db.dao.DtsGoodsCommissionConfigMapper;
import com.hzy.fivepalace.db.dao.DtsGoodsMapper;
import com.hzy.fivepalace.db.dao.DtsGoodsProductMapper;
import com.hzy.fivepalace.db.dao.DtsGoodsSpecificationMapper;
import com.hzy.fivepalace.db.dao.ex.CommentMapperEx;
import com.hzy.fivepalace.db.dao.ex.CommonMapperEx;
import com.hzy.fivepalace.db.dao.ex.DtsCategoryMapperEx;
import com.hzy.fivepalace.db.dao.ex.DtsGoodsMapperEx;
import com.hzy.fivepalace.db.domain.*;
import com.hzy.fivepalace.db.service.DtsGoodsProductService;
import com.hzy.fivepalace.db.vo.wx.WxGoodsSpecificationVo;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 商品服务
 */
@Service
public class AdminGoodsService extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdminGoodsService.class);

    @Resource
    private DtsGoodsMapperEx goodsMapperEx;

    @Resource
    private DtsGoodsMapper goodsMapper;

    @Resource
    private Redisson redisson;


    @Resource
    private DtsGoodsCommissionConfigMapper goodsCommissionConfigMapper;

    @Resource
    private CommentMapperEx commentMapperEx;

    @Autowired
    private DtsGoodsProductService productService;

    @Resource
    private DtsGoodsProductMapper dtsGoodsProductMapper;

    @Resource
    private DtsCategoryMapperEx categoryMapper;

    @Resource
    private DtsGoodsSpecificationMapper goodsSpecificationMapper;


    @Autowired
    private WxMaService wxMaService;

    @Resource
    private CommonMapperEx commonMapperEx;


    /**
     * 资源映射路径
     */
    @Value("${sys-config.profile}")
    private String profile;

    /**
     * 资源映射路径前缀
     */
    @Value("${sys-config.profile-handler}")
    private String profileHandler;

    /**
     * 域名
     */
    @Value("${sys-config.domain}")
    private String domain;

    @Value("${hzy.wx.app-id}")
    private String appId;


    private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);

    private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(16, 16, 1000, TimeUnit.MILLISECONDS,
            WORK_QUEUE, HANDLER);


    /**
     * 商品列表
     *
     * @param page
     * @param size
     * @param categoryId
     * @param content
     * @param isOnSale
     * @return
     */
    public TableDataInfo getGoodsList(Integer page, Integer size, Integer categoryId, String content, Boolean isOnSale) {
        Page p = PageHelper.startPage(page, size);
        List<DtsGoods> list = goodsMapperEx.getGoodsList(isOnSale, content, categoryId);
        List<Map<String, Object>> goodsList = new ArrayList<>();
        list.forEach(info -> {
            Map<String, Object> data = new HashMap<>();
            data.put("productList", new ArrayList<>());//货品信息列表不返回了
            data.put("goodsInfo", info);//商品详情
            data.put("goodsId", info.getId());//商品id
            data.put("commentCount", commentMapperEx.getCommentListByGoodsIdCount(info.getId()));//商品评论总数
            //获取商品总库存
            Integer inventory = goodsMapperEx.getGoodsInventory(info.getId());
            data.put("inventory", null == inventory ? 0 : inventory);
            //获取分类名称
            data.put("categoryName", goodsMapperEx.getCategoryNameByGoodsId(info.getId()));
            goodsList.add(data);
        });

        return TableDataInfo.getDataTable(goodsList, p.getTotal());
    }

    /**
     * 商品上架下架
     *
     * @param goodsId
     * @param isOnSale
     * @return
     */
    public Object updateOnSale(Integer goodsId, Boolean isOnSale) {
        RLock lock = redisson.getLock("admin_updateOnSale_" + goodsId);
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        DtsGoods info = goodsMapper.selectByPrimaryKey(goodsId);
        if (null == info || info.getDeleted()) {
            return ResponseUtil.badArgument("商品不存在或已删除");
        }
        info.setIsOnSale(isOnSale);
        info.setUpdateTime(LocalDateTime.now());
        goodsMapper.updateByPrimaryKeySelective(info);
        lock.unlock();
        return ResponseUtil.ok(isOnSale ? "上架成功" : "下架成功", null);
    }

    /**
     * 删除商品
     *
     * @param goodsId
     * @return
     */
    public Object delGoods(Integer goodsId) {
        RLock lock = redisson.getLock("admin_delGoods_" + goodsId);
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        DtsGoods info = goodsMapper.selectByPrimaryKey(goodsId);
        if (null == info) {
            return ResponseUtil.badArgument("商品不存在");
        }
        info.setDeleted(true);
        info.setUpdateTime(LocalDateTime.now());
        goodsMapper.updateByPrimaryKeySelective(info);
        lock.unlock();
        return ResponseUtil.ok("删除成功", null);
    }

    /**
     * 商品详情
     *
     * @param goodsId
     * @return
     */
    public Object detail(Integer goodsId) {
        // 商品信息
        DtsGoods info = goodsMapper.selectByPrimaryKey(goodsId);
        if (null == info || info.getDeleted()) {
            return ResponseUtil.badArgument("商品不存在或已删除");
        }
        //将不需要的字段设置为null
        info.setRetailPrice(null);
        info.setPicUrl(null);
        // 商品规格对应的数量和价格
        Callable<List<DtsGoodsProductService.specificationVo>> goodsProductsCallable = () -> productService.getGoodsProduct(goodsId,null,true, null);        FutureTask<List<DtsGoodsProductService.specificationVo>> goodsProductsListTask = new FutureTask<>(goodsProductsCallable);
        executorService.submit(goodsProductsListTask);
        Map<String, Object> data = new HashMap<>();
        try {
            if (info.getSpecificationType().intValue() == 1) {//单层规格类型
                data.put("productList", goodsProductsListTask.get());
            } else {//双层规格类型
                Callable<AdminUpdGoodsBothSpecificationMain> bothProductCallable = () -> getBothProductList(goodsProductsListTask.get());
                FutureTask<AdminUpdGoodsBothSpecificationMain> bothProductCallableTask = new FutureTask<>(bothProductCallable);
                executorService.submit(bothProductCallableTask);

                data.put("bothProductList", bothProductCallableTask.get());
            }
        } catch (Exception e) {
            logger.error("获取商品详情出错,商品id:{},msg:{}", goodsId, e.getMessage());
            return ResponseUtil.fail("获取商品详情出错");
        }
        //获取商品总库存
        Integer inventory = goodsMapperEx.getGoodsInventory(info.getId());
        data.put("inventory", null == inventory ? 0 : inventory);
        data.put("goodsInfo", info);//商品详情
        data.put("commentCount", commentMapperEx.getCommentListByGoodsIdCount(goodsId));//商品评论总数
        data.put("categoryName", null);
        data.put("categoryId", null);

        //获取新分类名称和id
        DtsCategory categoryNew = categoryMapper.selectByPrimaryKey(info.getCategoryIdNew());
        data.put("newCategoryName", null == categoryNew ? "" : categoryNew.getName());
        data.put("newCategoryId", info.getCategoryIdNew());

        //获取新分类父级名称
        DtsCategory pCategoryNameNew = categoryMapper.selectByPrimaryKey(null == categoryNew ? 0 : categoryNew.getPid());
        data.put("pNewCategoryName", null == pCategoryNameNew ? "" : pCategoryNameNew.getName());
        //获取新分类父级id
        data.put("pNewCategoryId", null == categoryNew ? 0 : categoryNew.getPid());

        return ResponseUtil.ok(data);
    }

    /**
     * 拼接双层规格详情
     *
     * @return
     */
    public AdminUpdGoodsBothSpecificationMain getBothProductList(List<DtsGoodsProductService.specificationVo> list) {
        AdminUpdGoodsBothSpecificationMain main = new AdminUpdGoodsBothSpecificationMain();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //拼接主规格名称和子规格名称
        main.setSpecificationMainName(list.get(0).getName());
        main.setSpecificationChildName(list.get(1).getName());
        //拼接规格参数
        List<AdminUpdGoodsBothSpecification> parameterList = new ArrayList<>();
        for (WxGoodsSpecificationVo vo : list.get(0).getValueList()) {//先循环拼接主规格
            AdminUpdGoodsBothSpecification bothSpecification = new AdminUpdGoodsBothSpecification();
            bothSpecification.setSpecificationId(vo.getSpecificationId());
            bothSpecification.setSpecificationValue(vo.getSpecificationValue());
            bothSpecification.setSpecificationImg(vo.getSpecificationImg());
            bothSpecification.setPrice(vo.getPrice());

            bothSpecification.setOneLevelExtendScaleVipType1(vo.getOneLevelExtendScaleVipType1());
            bothSpecification.setTwoLevelExtendScaleVipType1(vo.getTwoLevelExtendScaleVipType1());

            bothSpecification.setOneLevelExtendScaleVipType2(vo.getOneLevelExtendScaleVipType2());
            bothSpecification.setTwoLevelExtendScaleVipType2(vo.getTwoLevelExtendScaleVipType2());

            bothSpecification.setOneLevelExtendScaleVipType3(vo.getOneLevelExtendScaleVipType3());
            bothSpecification.setTwoLevelExtendScaleVipType3(vo.getTwoLevelExtendScaleVipType3());

            bothSpecification.setOneLevelExtendScaleVipType4(vo.getOneLevelExtendScaleVipType4());
            bothSpecification.setTwoLevelExtendScaleVipType4(vo.getTwoLevelExtendScaleVipType4());

            List<AdminUpdGoodsBothSpecificationChild> childParameterList = new ArrayList<>();
            for (WxGoodsSpecificationVo childVo : list.get(1).getValueList()) {//再循环拼接子规格参数
                if (childVo.getPid().equals(vo.getSpecificationId())) {
                    AdminUpdGoodsBothSpecificationChild childParameter = new AdminUpdGoodsBothSpecificationChild();
                    childParameter.setSpecificationId(childVo.getSpecificationId());
                    childParameter.setSpecificationValue(childVo.getSpecificationValue());
                    childParameter.setNumber(childVo.getNumber());
                    childParameterList.add(childParameter);
                }
            }
            bothSpecification.setChildParameterList(childParameterList);
            parameterList.add(bothSpecification);
        }
        main.setParameterList(parameterList);
        return main;
    }

    /**
     * 新增商品
     *
     * @param dto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object addGoods(AdminAddGoods dto) {
        RLock lock = redisson.getLock("admin_addGoods_" + getAdminId());
        if (lock.isLocked()) {
            return ResponseUtil.frequent();
        }
        lock.lock(30, TimeUnit.SECONDS);

        //入参校验
        if (StringUtils.isBlank(dto.getName())) {
            lock.unlock();
            return ResponseUtil.fail("商品名称不能为空");
        }

        if (dto.getName().length() > 150) {
            lock.unlock();
            return ResponseUtil.fail("商品名称最多150个字");
        }

        if (null == dto.getCounterPrice() || dto.getCounterPrice().compareTo(new BigDecimal(0)) <= 0) {
            lock.unlock();
            return ResponseUtil.fail("商品原价不能为空并且必须大于0");
        }

        if (null == dto.getCategoryIdNew()) {
            lock.unlock();
            return ResponseUtil.fail("请选择分类");
        }
        DtsCategory category = categoryMapper.selectByPrimaryKey(dto.getCategoryIdNew());
        if (null == category || category.getDeleted() ||category.getIsHome()) {
            lock.unlock();
            return ResponseUtil.fail("参数【categoryIdNew】错误");
        }

        if (null == dto.getGallery() || dto.getGallery().length < 1) {
            lock.unlock();
            return ResponseUtil.fail("商品宣传图片为空");
        }
        if (!StringUtils.isBlank(dto.getKeywords()) && dto.getKeywords().length() > 200) {
            lock.unlock();
            return ResponseUtil.fail("商品关键字长度最多200");
        }

        if (!StringUtils.isBlank(dto.getBrief()) && dto.getBrief().length() > 300) {
            lock.unlock();
            return ResponseUtil.fail("商品简介最多300个字");
        }

        if (StringUtils.isBlank(dto.getDetail())) {
            lock.unlock();
            return ResponseUtil.fail("商品详情介绍不能为空");
        }

        if (null == dto.getSortOrder() || dto.getSortOrder() <= 0) {
            dto.setSortOrder((short) 0);
        }
        if (!StringUtils.isBlank(dto.getGoodsSn()) && dto.getGoodsSn().length() > 50) {
            lock.unlock();
            return ResponseUtil.fail("商品编码最多50位");
        }

        if (!StringUtils.isBlank(dto.getUnit()) && dto.getUnit().length() > 10) {
            lock.unlock();
            return ResponseUtil.fail("商品单位最多10个字");
        }


        if (null == dto.getSpecificationType()) {
            lock.unlock();
            return ResponseUtil.fail("请选择规格类型");
        }
        if (dto.getSpecificationType().intValue() != 1 && dto.getSpecificationType().intValue() != 2) {
            lock.unlock();
            return ResponseUtil.fail("规格类型错误");
        }
        if (dto.getSpecificationType().intValue() == 1) {//单层规格
            if (CollectionUtils.isEmpty(dto.getSpecificationList())) {
                lock.unlock();
                return ResponseUtil.fail("未添加商品规格");
            }

            if (dto.getSpecificationList().size() == 1) {
                AdminAddGoodsSpecification specification = dto.getSpecificationList().get(0);
                if (null == specification.getPrice()) {
                    lock.unlock();
                    return ResponseUtil.fail("商品规格中价格不能为空");
                }
                if (null == specification.getNumber()) {
                    dto.getSpecificationList().get(0).setNumber(0);
                }
                //规格值为空就默认一个值
                if (StringUtils.isBlank(specification.getSpecificationValue())) {
                    dto.getSpecificationList().get(0).setSpecificationValue("默认规格");
                }
                //规格图片为空就取商品宣传图片的第一个
                if (StringUtils.isBlank(specification.getSpecificationImg())) {
                    dto.getSpecificationList().get(0).setSpecificationImg(dto.getGallery()[0]);
                }
            } else {
                for (AdminAddGoodsSpecification specification : dto.getSpecificationList()) {
                    if (null == specification.getPrice()) {
                        lock.unlock();
                        return ResponseUtil.fail("商品规格中价格为空");
                    }
                    if (StringUtils.isBlank(specification.getSpecificationValue())) {
                        lock.unlock();
                        return ResponseUtil.fail("商品规格中规格值为空");
                    }
                    if (StringUtils.isBlank(specification.getSpecificationImg())) {
                        lock.unlock();
                        return ResponseUtil.fail("商品规格中规格图片为空");
                    }
                    if (null == specification.getNumber()) {
                        specification.setNumber(0);
                    }
                }
            }
        } else {//双层规格

            //入参判断
            if (null == dto.getBothSpecification()) {
                lock.unlock();
                return ResponseUtil.fail("未添加商品规格");
            }
            if (StringUtils.isBlank(dto.getBothSpecification().getSpecificationMainName())) {
                lock.unlock();
                return ResponseUtil.fail("主规格名称为空");
            }
            if (StringUtils.isBlank(dto.getBothSpecification().getSpecificationChildName())) {
                lock.unlock();
                return ResponseUtil.fail("子规格名称为空");
            }
            if (CollectionUtils.isEmpty(dto.getBothSpecification().getParameterList())) {
                lock.unlock();
                return ResponseUtil.fail("规格参数为空");
            }
            for (AdminAddGoodsBothSpecification specification : dto.getBothSpecification().getParameterList()) {
                if (null == specification.getPrice()) {
                    lock.unlock();
                    return ResponseUtil.fail("规格中价格为空");
                }
                if (StringUtils.isBlank(specification.getSpecificationValue())) {
                    lock.unlock();
                    return ResponseUtil.fail("规格中规格值为空");
                }
                if (StringUtils.isBlank(specification.getSpecificationImg())) {
                    lock.unlock();
                    return ResponseUtil.fail("规格中规格图片为空");
                }
                if (CollectionUtils.isEmpty(specification.getChildParameterList())) {
                    lock.unlock();
                    return ResponseUtil.fail("规格中子规格参数为空");
                }
                for (AdminAddGoodsBothSpecificationChild childSpecification : specification.getChildParameterList()) {
                    if (StringUtils.isBlank(childSpecification.getSpecificationValue())) {
                        lock.unlock();
                        return ResponseUtil.fail("规格中子规格值为空");
                    }
                    if (null == childSpecification.getNumber()) {
                        childSpecification.setNumber(0);
                    }
                }
            }
        }


        DtsGoods goodsAadd = new DtsGoods();
        BeanUtils.copyProperties(dto, goodsAadd);
        goodsAadd.setUpdateTime(LocalDateTime.now());
        goodsAadd.setAddTime(LocalDateTime.now());
        goodsAadd.setDeleted(false);
        goodsAadd.setPicUrl(dto.getGallery()[0]);
        goodsAadd.setSpecificationType(dto.getSpecificationType());
        goodsMapper.insertSelective(goodsAadd);
        //处理分享url
        //创建微信小程序二维码
        try {
            File file = wxMaService.switchoverTo(appId).getQrcodeService().createWxaCodeUnlimit(goodsAadd.getId().toString(), "pages/shangpinxiangqing/shangpinxiangqing");
            String realFileName = System.currentTimeMillis() + ".png";
            BufferedImage read = ImageIO.read(file);
            if (read != null) {
                ImageIO.write(read, "png", new File(profile + "/goods_file/" + realFileName));//有就写入
            } else {
                throw new AppException("图片为空");
            }
            String filePath = domain + profileHandler + "/goods_file/" + realFileName;
            goodsAadd.setShareUrl(filePath);
            goodsMapper.updateByPrimaryKeySelective(goodsAadd);
        } catch (Exception e) {
            lock.unlock();
            logger.error("新增商品失败,创建小程序码异常:{}", e.getMessage());
            return ResponseUtil.fail("创建小程序码失败");
        }

        Integer goodsId = goodsAadd.getId();
        if (dto.getSpecificationType().intValue() == 1) {//单层规格
            //货品和规格入库
            for (AdminAddGoodsSpecification specification : dto.getSpecificationList()) {
                //货品入库
                DtsGoodsProduct product = new DtsGoodsProduct();
                product.setGoodsId(goodsId);
                product.setPrice(specification.getPrice());
                product.setNumber(specification.getNumber());
                product.setUrl(specification.getSpecificationImg());
                product.setDeleted(false);
                product.setAddTime(LocalDateTime.now());
                product.setUpdateTime(LocalDateTime.now());
                dtsGoodsProductMapper.insertSelective(product);
                Integer productId = product.getId();
                if(null!=specification.getOneLevelExtendScaleVipType1()||null!=specification.getTwoLevelExtendScaleVipType1()){
                    if(null==specification.getOneLevelExtendScaleVipType1()){
                        specification.setOneLevelExtendScaleVipType1(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType1()){
                        specification.setTwoLevelExtendScaleVipType1(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType1());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType1());
                    record.setVipType(VipTypeEnums.TYPE1.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType2()||null!=specification.getTwoLevelExtendScaleVipType2()){
                    if(null==specification.getOneLevelExtendScaleVipType2()){
                        specification.setOneLevelExtendScaleVipType2(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType2()){
                        specification.setTwoLevelExtendScaleVipType2(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType2());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType2());
                    record.setVipType(VipTypeEnums.TYPE2.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType3()||null!=specification.getTwoLevelExtendScaleVipType3()){
                    if(null==specification.getOneLevelExtendScaleVipType3()){
                        specification.setOneLevelExtendScaleVipType3(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType3()){
                        specification.setTwoLevelExtendScaleVipType3(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType3());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType3());
                    record.setVipType(VipTypeEnums.TYPE3.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType4()||null!=specification.getTwoLevelExtendScaleVipType4()){
                    if(null==specification.getOneLevelExtendScaleVipType4()){
                        specification.setOneLevelExtendScaleVipType4(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType4()){
                        specification.setTwoLevelExtendScaleVipType4(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType4());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType4());
                    record.setVipType(VipTypeEnums.TYPE4.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }


                //规格入库
                DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                goodsSpecification.setGoodsId(goodsId);
                goodsSpecification.setSpecification("规格");
                goodsSpecification.setValue(specification.getSpecificationValue());
                goodsSpecification.setPicUrl(specification.getSpecificationImg());
                goodsSpecification.setGoodsPoroductId(productId);
                goodsSpecification.setAddTime(LocalDateTime.now());
                goodsSpecification.setUpdateTime(LocalDateTime.now());
                goodsSpecification.setPid(0);
                goodsSpecification.setDeleted(false);
                goodsSpecificationMapper.insertSelective(goodsSpecification);
            }
        } else {//双层规格

            //货品和规格入库
            for (AdminAddGoodsBothSpecification specification : dto.getBothSpecification().getParameterList()) {
                //货品入库
                DtsGoodsProduct product = new DtsGoodsProduct();
                product.setGoodsId(goodsId);
                product.setPrice(specification.getPrice());
                product.setUrl(specification.getSpecificationImg());
                product.setDeleted(false);
                product.setAddTime(LocalDateTime.now());
                product.setUpdateTime(LocalDateTime.now());
                dtsGoodsProductMapper.insertSelective(product);
                //货品id
                Integer productId = product.getId();


                if(null!=specification.getOneLevelExtendScaleVipType1()||null!=specification.getTwoLevelExtendScaleVipType1()){
                    if(null==specification.getOneLevelExtendScaleVipType1()){
                        specification.setOneLevelExtendScaleVipType1(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType1()){
                        specification.setTwoLevelExtendScaleVipType1(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType1());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType1());
                    record.setVipType(VipTypeEnums.TYPE1.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType2()||null!=specification.getTwoLevelExtendScaleVipType2()){
                    if(null==specification.getOneLevelExtendScaleVipType2()){
                        specification.setOneLevelExtendScaleVipType2(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType2()){
                        specification.setTwoLevelExtendScaleVipType2(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType2());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType2());
                    record.setVipType(VipTypeEnums.TYPE2.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType3()||null!=specification.getTwoLevelExtendScaleVipType3()){
                    if(null==specification.getOneLevelExtendScaleVipType3()){
                        specification.setOneLevelExtendScaleVipType3(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType3()){
                        specification.setTwoLevelExtendScaleVipType3(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType3());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType3());
                    record.setVipType(VipTypeEnums.TYPE3.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType4()||null!=specification.getTwoLevelExtendScaleVipType4()){
                    if(null==specification.getOneLevelExtendScaleVipType4()){
                        specification.setOneLevelExtendScaleVipType4(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType4()){
                        specification.setTwoLevelExtendScaleVipType4(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType4());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType4());
                    record.setVipType(VipTypeEnums.TYPE4.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }



                //主规格入库
                DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                goodsSpecification.setGoodsId(goodsId);
                //主规格名称
                goodsSpecification.setSpecification(dto.getBothSpecification().getSpecificationMainName());
                goodsSpecification.setValue(specification.getSpecificationValue());
                goodsSpecification.setPicUrl(specification.getSpecificationImg());
                goodsSpecification.setGoodsPoroductId(productId);
                goodsSpecification.setAddTime(LocalDateTime.now());
                goodsSpecification.setUpdateTime(LocalDateTime.now());
                goodsSpecification.setDeleted(false);
                //父id为0
                goodsSpecification.setPid(0);
                goodsSpecificationMapper.insertSelective(goodsSpecification);
                //主规格id
                Integer specificationPid = goodsSpecification.getId();

                //子规格入库
                for (AdminAddGoodsBothSpecificationChild childSpecification : specification.getChildParameterList()) {
                    //货品入库
                    DtsGoodsProduct childProduct = new DtsGoodsProduct();
                    childProduct.setGoodsId(goodsId);
                    childProduct.setNumber(childSpecification.getNumber());
                    childProduct.setDeleted(false);
                    childProduct.setAddTime(LocalDateTime.now());
                    childProduct.setUpdateTime(LocalDateTime.now());
                    dtsGoodsProductMapper.insertSelective(childProduct);
                    //货品id
                    Integer childProductId = childProduct.getId();

                    DtsGoodsSpecification goodsSpecificationChild = new DtsGoodsSpecification();
                    goodsSpecificationChild.setGoodsId(goodsId);
                    //子规格名称
                    goodsSpecificationChild.setSpecification(dto.getBothSpecification().getSpecificationChildName());
                    goodsSpecificationChild.setValue(childSpecification.getSpecificationValue());
                    goodsSpecificationChild.setAddTime(LocalDateTime.now());
                    goodsSpecificationChild.setUpdateTime(LocalDateTime.now());
                    goodsSpecificationChild.setDeleted(false);
                    //父id就是主规格id
                    goodsSpecificationChild.setPid(specificationPid);
                    goodsSpecificationChild.setGoodsPoroductId(childProductId);
                    goodsSpecificationMapper.insertSelective(goodsSpecificationChild);
                }
            }

        }

        lock.unlock();
        return ResponseUtil.ok("新增成功", null);
    }

    /**
     * 修改商品
     *
     * @param dto
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object updGoods(AdminUpdGoods dto) {
        if (null == dto.getId() || dto.getId().intValue() < 1) {
            return ResponseUtil.fail("参数【id】为空");
        }
        RLock lock = redisson.getLock("admin_updGoods_" + dto.getId());
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);

        //入参校验
        if (StringUtils.isBlank(dto.getName())) {
            dto.setName(null);
        }
        if (!StringUtils.isBlank(dto.getName()) && dto.getName().length() > 150) {
            lock.unlock();
            return ResponseUtil.fail("商品名称最多150个字");
        }
        if (null == dto.getCounterPrice() || dto.getCounterPrice().compareTo(new BigDecimal(0)) <= 0) {
            dto.setCounterPrice(null);
        }
        if (null != dto.getCategoryIdNew()) {//校验新分类
            DtsCategory category = categoryMapper.selectByPrimaryKey(dto.getCategoryIdNew());
            if (null == category || category.getDeleted() ||category.getIsHome()) {
                lock.unlock();
                return ResponseUtil.fail("参数【categoryIdNew】错误");
            }
        }

        if (null == dto.getGallery() || dto.getGallery().length < 1) {
            dto.setGallery(null);
        }
        if (StringUtils.isBlank(dto.getKeywords())) {
            dto.setKeywords(null);
        }
        if (!StringUtils.isBlank(dto.getKeywords()) && dto.getKeywords().length() > 200) {
            lock.unlock();
            return ResponseUtil.fail("商品关键字长度最多200");
        }
        if (StringUtils.isBlank(dto.getBrief())) {
            dto.setBrief(null);
        }
        if (!StringUtils.isBlank(dto.getBrief()) && dto.getBrief().length() > 300) {
            lock.unlock();
            return ResponseUtil.fail("商品简介最多300个字");
        }

        if (StringUtils.isBlank(dto.getDetail())) {
            dto.setDetail(null);
        }

        if (null == dto.getSortOrder() || dto.getSortOrder() <= 0) {
            dto.setSortOrder(null);
        }
        if (StringUtils.isBlank(dto.getGoodsSn())) {
            dto.setGoodsSn(null);
        }
        if (!StringUtils.isBlank(dto.getGoodsSn()) && dto.getGoodsSn().length() > 50) {
            lock.unlock();
            return ResponseUtil.fail("商品编码最多50位");
        }
        if (StringUtils.isBlank(dto.getUnit())) {
            dto.setUnit(null);
        }
        if (!StringUtils.isBlank(dto.getUnit()) && dto.getUnit().length() > 10) {
            lock.unlock();
            return ResponseUtil.fail("商品单位最多10个字");
        }

        DtsGoods oldGoods = goodsMapper.selectByPrimaryKey(dto.getId());
        if (null == oldGoods || oldGoods.getDeleted()) {
            return ResponseUtil.badArgument("商品不存在或已删除");
        }

        if (oldGoods.getSpecificationType().intValue() == 1) {//单层规格
            //处理规格数据
            if (!CollectionUtils.isEmpty(dto.getSpecificationList())) {
                if (dto.getSpecificationList().size() == 1) {
                    AdminUpdGoodsSpecification specification = dto.getSpecificationList().get(0);
                    if (null == specification.getSpecificationId() || specification.getSpecificationId() < 1) {//新增的规格参数判断
                        if (null == specification.getPrice()) {
                            lock.unlock();
                            return ResponseUtil.fail("商品规格中价格不能为空");
                        }
                        if (null == specification.getNumber()) {
                            dto.getSpecificationList().get(0).setNumber(0);
                        }
                        //规格值为空就默认一个值
                        if (StringUtils.isBlank(specification.getSpecificationValue())) {
                            dto.getSpecificationList().get(0).setSpecificationValue("默认规格");
                        }
                        //规格图片为空就取商品宣传图片的第一个
                        if (StringUtils.isBlank(specification.getSpecificationImg())) {
                            dto.getSpecificationList().get(0).setSpecificationImg(dto.getGallery()[0]);
                        }
                    } else {//不是新增的规格处理
                        if (StringUtils.isBlank(specification.getSpecificationValue())) {
                            dto.getSpecificationList().get(0).setSpecificationValue(null);
                        }
                        if (StringUtils.isBlank(specification.getSpecificationImg())) {
                            dto.getSpecificationList().get(0).setSpecificationImg(null);
                        }
                    }

                } else {
                    for (AdminUpdGoodsSpecification specification : dto.getSpecificationList()) {
                        if (null == specification.getSpecificationId() || specification.getSpecificationId() < 1) {//新增的规格参数判断
                            if (null == specification.getPrice()) {
                                lock.unlock();
                                return ResponseUtil.fail("商品规格中存在价格为空");
                            }
                            if (StringUtils.isBlank(specification.getSpecificationValue())) {
                                lock.unlock();
                                return ResponseUtil.fail("商品规格中存在规格值为空");
                            }
                            if (StringUtils.isBlank(specification.getSpecificationImg())) {
                                lock.unlock();
                                return ResponseUtil.fail("商品规格中存在规格图片为空");
                            }
                            if (null == specification.getNumber()) {
                                specification.setNumber(0);
                            }
                        } else {//不是新增的规格处理
                            if (StringUtils.isBlank(specification.getSpecificationValue())) {
                                specification.setSpecificationValue(null);
                            }
                            if (StringUtils.isBlank(specification.getSpecificationImg())) {
                                specification.setSpecificationImg(null);
                            }
                        }
                    }
                }
            } else {//如果为空判断该商品是否有规格信息
                DtsGoodsProductExample goodsProductExample = new DtsGoodsProductExample();
                goodsProductExample.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(dto.getId());
                if (dtsGoodsProductMapper.countByExample(goodsProductExample) < 1) {
                    lock.unlock();
                    return ResponseUtil.fail("修改失败,未添加商品规格数据");
                }
            }
        } else {//双层规格

            //入参判断
            if (null == dto.getBothSpecification()) {
                lock.unlock();
                return ResponseUtil.fail("未添加商品规格");
            }
            if (StringUtils.isBlank(dto.getBothSpecification().getSpecificationMainName())) {
                lock.unlock();
                return ResponseUtil.fail("主规格名称为空");
            }
            if (StringUtils.isBlank(dto.getBothSpecification().getSpecificationChildName())) {
                lock.unlock();
                return ResponseUtil.fail("子规格名称为空");
            }
            if (CollectionUtils.isEmpty(dto.getBothSpecification().getParameterList())) {
                lock.unlock();
                return ResponseUtil.fail("规格参数为空");
            }
            for (AdminUpdGoodsBothSpecification specification : dto.getBothSpecification().getParameterList()) {
                if (null == specification.getSpecificationId() || specification.getSpecificationId() < 1) {//新增的规格参数判断
                    if (null == specification.getPrice()) {
                        lock.unlock();
                        return ResponseUtil.fail("规格中价格为空");
                    }
                    if (StringUtils.isBlank(specification.getSpecificationValue())) {
                        lock.unlock();
                        return ResponseUtil.fail("规格中规格值为空");
                    }
                    if (StringUtils.isBlank(specification.getSpecificationImg())) {
                        lock.unlock();
                        return ResponseUtil.fail("规格中规格图片为空");
                    }
                    if (CollectionUtils.isEmpty(specification.getChildParameterList())) {
                        lock.unlock();
                        return ResponseUtil.fail("规格中子规格参数为空");
                    }
                    for (AdminUpdGoodsBothSpecificationChild childSpecification : specification.getChildParameterList()) {
                        if (StringUtils.isBlank(childSpecification.getSpecificationValue())) {
                            lock.unlock();
                            return ResponseUtil.fail("规格中子规格值为空");
                        }
                        if (null == childSpecification.getNumber()) {
                            childSpecification.setNumber(0);
                        }
                    }

                } else {//不是新增的规格处理

                    if (StringUtils.isBlank(specification.getSpecificationValue())) {
                        specification.setSpecificationValue(null);
                    }
                    if (StringUtils.isBlank(specification.getSpecificationImg())) {
                        specification.setSpecificationImg(null);
                    }
                    if (!CollectionUtils.isEmpty(specification.getChildParameterList())) {
                        for (AdminUpdGoodsBothSpecificationChild childSpecification : specification.getChildParameterList()) {
                            if (null == childSpecification.getSpecificationId() || childSpecification.getSpecificationId() < 1) {//新增的规格参数判断
                                if (StringUtils.isBlank(childSpecification.getSpecificationValue())) {
                                    lock.unlock();
                                    return ResponseUtil.fail("规格中子规格值为空");
                                }
                            } else {//不是新增的规格参数
                                if (StringUtils.isBlank(childSpecification.getSpecificationValue())) {
                                    childSpecification.setSpecificationValue(null);
                                }
                            }
                        }
                    }

                }
            }


        }


        if (null == dto.getGallery() || dto.getGallery().length < 1) {
            dto.setGallery(oldGoods.getGallery());
        }

        DtsGoods goodsUpd = new DtsGoods();
        BeanUtils.copyProperties(dto, goodsUpd);
        goodsUpd.setId(oldGoods.getId());
        goodsUpd.setDeleted(false);
        goodsUpd.setPicUrl(dto.getGallery()[0]);
        goodsUpd.setUpdateTime(LocalDateTime.now());
        goodsMapper.updateByPrimaryKeySelective(goodsUpd);
        Integer goodsId = oldGoods.getId();
        if (oldGoods.getSpecificationType().intValue() == 1) {//单层规格
            //货品和规格入库
            if (!CollectionUtils.isEmpty(dto.getSpecificationList())) {
                for (AdminUpdGoodsSpecification specification : dto.getSpecificationList()) {
                    if (null == specification.getSpecificationId() || specification.getSpecificationId() < 1) {
                        specification.setSpecificationId(0);
                    }
                    Integer productId =null;
                    //先查库
                    DtsGoodsSpecification oldGoodsSpecification = goodsSpecificationMapper.selectByPrimaryKey(specification.getSpecificationId());
                    if (null == oldGoodsSpecification) {//新增
                        //货品入库
                        DtsGoodsProduct product = new DtsGoodsProduct();
                        product.setGoodsId(goodsId);
                        product.setPrice(specification.getPrice());
                        product.setNumber(specification.getNumber());
                        product.setUrl(specification.getSpecificationImg());
                        product.setDeleted(false);
                        product.setAddTime(LocalDateTime.now());
                        product.setUpdateTime(LocalDateTime.now());
                        dtsGoodsProductMapper.insertSelective(product);
                        productId = product.getId();
                        //规格入库
                        DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                        goodsSpecification.setGoodsId(goodsId);
                        goodsSpecification.setSpecification("规格");
                        goodsSpecification.setValue(specification.getSpecificationValue());
                        goodsSpecification.setPicUrl(specification.getSpecificationImg());
                        goodsSpecification.setGoodsPoroductId(productId);
                        goodsSpecification.setAddTime(LocalDateTime.now());
                        goodsSpecification.setUpdateTime(LocalDateTime.now());
                        goodsSpecification.setDeleted(false);
                        goodsSpecificationMapper.insertSelective(goodsSpecification);
                    } else {//修改
                        productId=oldGoodsSpecification.getGoodsPoroductId();
                        //货品修改入库
                        DtsGoodsProduct product = new DtsGoodsProduct();
                        product.setId(oldGoodsSpecification.getGoodsPoroductId());
                        product.setGoodsId(goodsId);
                        product.setPrice(specification.getPrice());
                        product.setNumber(specification.getNumber());
                        product.setUrl(specification.getSpecificationImg());
                        product.setUpdateTime(LocalDateTime.now());
                        dtsGoodsProductMapper.updateByPrimaryKeySelective(product);
                        //规格修改入库
                        DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                        goodsSpecification.setId(oldGoodsSpecification.getId());
                        goodsSpecification.setGoodsId(goodsId);
                        goodsSpecification.setValue(specification.getSpecificationValue());
                        goodsSpecification.setPicUrl(specification.getSpecificationImg());
                        goodsSpecification.setGoodsPoroductId(oldGoodsSpecification.getGoodsPoroductId());
                        goodsSpecification.setUpdateTime(LocalDateTime.now());
                        goodsSpecificationMapper.updateByPrimaryKeySelective(goodsSpecification);
                    }



                    if(null!=specification.getOneLevelExtendScaleVipType1()||null!=specification.getTwoLevelExtendScaleVipType1()){
                        if(null==specification.getOneLevelExtendScaleVipType1()){
                            specification.setOneLevelExtendScaleVipType1(new BigDecimal("-1098"));
                        }
                        if(null==specification.getTwoLevelExtendScaleVipType1()){
                            specification.setTwoLevelExtendScaleVipType1(new BigDecimal("-1098"));
                        }
                        DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                        record.setGoodsPoroductId(productId);
                        record.setAddTime(LocalDateTime.now());
                        record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType1());
                        record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType1());
                        record.setVipType(VipTypeEnums.TYPE1.getId());
                        goodsCommissionConfigMapper.insertSelective(record);
                    }
                    if(null!=specification.getOneLevelExtendScaleVipType2()||null!=specification.getTwoLevelExtendScaleVipType2()){
                        if(null==specification.getOneLevelExtendScaleVipType2()){
                            specification.setOneLevelExtendScaleVipType2(new BigDecimal("-1098"));
                        }
                        if(null==specification.getTwoLevelExtendScaleVipType2()){
                            specification.setTwoLevelExtendScaleVipType2(new BigDecimal("-1098"));
                        }
                        DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                        record.setGoodsPoroductId(productId);
                        record.setAddTime(LocalDateTime.now());
                        record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType2());
                        record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType2());
                        record.setVipType(VipTypeEnums.TYPE2.getId());
                        goodsCommissionConfigMapper.insertSelective(record);
                    }
                    if(null!=specification.getOneLevelExtendScaleVipType3()||null!=specification.getTwoLevelExtendScaleVipType3()){
                        if(null==specification.getOneLevelExtendScaleVipType3()){
                            specification.setOneLevelExtendScaleVipType3(new BigDecimal("-1098"));
                        }
                        if(null==specification.getTwoLevelExtendScaleVipType3()){
                            specification.setTwoLevelExtendScaleVipType3(new BigDecimal("-1098"));
                        }
                        DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                        record.setGoodsPoroductId(productId);
                        record.setAddTime(LocalDateTime.now());
                        record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType3());
                        record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType3());
                        record.setVipType(VipTypeEnums.TYPE3.getId());
                        goodsCommissionConfigMapper.insertSelective(record);
                    }
                    if(null!=specification.getOneLevelExtendScaleVipType4()||null!=specification.getTwoLevelExtendScaleVipType4()){
                        if(null==specification.getOneLevelExtendScaleVipType4()){
                            specification.setOneLevelExtendScaleVipType4(new BigDecimal("-1098"));
                        }
                        if(null==specification.getTwoLevelExtendScaleVipType4()){
                            specification.setTwoLevelExtendScaleVipType4(new BigDecimal("-1098"));
                        }
                        DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                        record.setGoodsPoroductId(productId);
                        record.setAddTime(LocalDateTime.now());
                        record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType4());
                        record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType4());
                        record.setVipType(VipTypeEnums.TYPE4.getId());
                        goodsCommissionConfigMapper.insertSelective(record);
                    }
                }
            }
        } else {//双层规格入库


            //货品和规格入库
            for (AdminUpdGoodsBothSpecification specification : dto.getBothSpecification().getParameterList()) {
                if (null == specification.getSpecificationId() || specification.getSpecificationId() < 1) {
                    specification.setSpecificationId(0);
                }
                Integer productId =null;
                //先查库
                DtsGoodsSpecification oldGoodsSpecification = goodsSpecificationMapper.selectByPrimaryKey(specification.getSpecificationId());
                if (null == oldGoodsSpecification) {//新增

                    //货品入库
                    DtsGoodsProduct product = new DtsGoodsProduct();
                    product.setGoodsId(goodsId);
                    product.setPrice(specification.getPrice());
                    product.setUrl(specification.getSpecificationImg());
                    product.setDeleted(false);
                    product.setAddTime(LocalDateTime.now());
                    product.setUpdateTime(LocalDateTime.now());
                    dtsGoodsProductMapper.insertSelective(product);
                    //货品id
                    productId = product.getId();

                    //主规格入库
                    DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                    goodsSpecification.setGoodsId(goodsId);
                    //主规格名称
                    goodsSpecification.setSpecification(dto.getBothSpecification().getSpecificationMainName());
                    goodsSpecification.setValue(specification.getSpecificationValue());
                    goodsSpecification.setPicUrl(specification.getSpecificationImg());
                    goodsSpecification.setGoodsPoroductId(productId);
                    goodsSpecification.setAddTime(LocalDateTime.now());
                    goodsSpecification.setUpdateTime(LocalDateTime.now());
                    goodsSpecification.setDeleted(false);
                    //父id为0
                    goodsSpecification.setPid(0);
                    goodsSpecificationMapper.insertSelective(goodsSpecification);
                    //主规格id
                    Integer specificationPid = goodsSpecification.getId();

                    //子规格入库
                    for (AdminUpdGoodsBothSpecificationChild childSpecification : specification.getChildParameterList()) {
                        //货品入库
                        DtsGoodsProduct childProduct = new DtsGoodsProduct();
                        childProduct.setGoodsId(goodsId);
                        childProduct.setNumber(childSpecification.getNumber());
                        childProduct.setDeleted(false);
                        childProduct.setAddTime(LocalDateTime.now());
                        childProduct.setUpdateTime(LocalDateTime.now());
                        dtsGoodsProductMapper.insertSelective(childProduct);
                        //货品id
                        Integer childProductId = childProduct.getId();


                        DtsGoodsSpecification goodsSpecificationChild = new DtsGoodsSpecification();
                        goodsSpecificationChild.setGoodsId(goodsId);
                        //子规格名称
                        goodsSpecificationChild.setSpecification(dto.getBothSpecification().getSpecificationChildName());
                        goodsSpecificationChild.setValue(childSpecification.getSpecificationValue());
                        goodsSpecificationChild.setAddTime(LocalDateTime.now());
                        goodsSpecificationChild.setUpdateTime(LocalDateTime.now());
                        goodsSpecificationChild.setDeleted(false);
                        //父id就是主规格id
                        goodsSpecificationChild.setPid(specificationPid);
                        goodsSpecificationChild.setGoodsPoroductId(childProductId);
                        goodsSpecificationMapper.insertSelective(goodsSpecificationChild);
                    }

                } else {//修改
                    //货品入库
                    DtsGoodsProduct product = new DtsGoodsProduct();
                    product.setId(oldGoodsSpecification.getGoodsPoroductId());
                    product.setGoodsId(goodsId);
                    product.setPrice(specification.getPrice());
                    product.setUrl(specification.getSpecificationImg());
                    product.setUpdateTime(LocalDateTime.now());
                    dtsGoodsProductMapper.updateByPrimaryKeySelective(product);
                    //货品id
                    productId = product.getId();

                    //主规格入库
                    DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
                    goodsSpecification.setId(oldGoodsSpecification.getId());
                    goodsSpecification.setGoodsId(goodsId);
                    //主规格名称
                    goodsSpecification.setSpecification(dto.getBothSpecification().getSpecificationMainName());
                    goodsSpecification.setValue(specification.getSpecificationValue());
                    goodsSpecification.setPicUrl(specification.getSpecificationImg());
                    goodsSpecification.setGoodsPoroductId(productId);
                    goodsSpecification.setUpdateTime(LocalDateTime.now());
                    //父id为0
                    goodsSpecification.setPid(0);
                    goodsSpecificationMapper.updateByPrimaryKeySelective(goodsSpecification);
                    //主规格id
                    Integer specificationPid = goodsSpecification.getId();

                    //子规格入库
                    for (AdminUpdGoodsBothSpecificationChild childSpecification : specification.getChildParameterList()) {
                        if (null == childSpecification.getSpecificationId() || childSpecification.getSpecificationId() < 1) {
                            childSpecification.setSpecificationId(0);
                        }
                        //先查库
                        DtsGoodsSpecification childOldGoodsSpecification = goodsSpecificationMapper.selectByPrimaryKey(childSpecification.getSpecificationId());
                        if (null == childOldGoodsSpecification) {//新增
                            //货品入库
                            DtsGoodsProduct childProduct = new DtsGoodsProduct();
                            childProduct.setGoodsId(goodsId);
                            childProduct.setNumber(childSpecification.getNumber());
                            childProduct.setDeleted(false);
                            childProduct.setAddTime(LocalDateTime.now());
                            childProduct.setUpdateTime(LocalDateTime.now());
                            dtsGoodsProductMapper.insertSelective(childProduct);
                            //货品id
                            Integer childProductId = childProduct.getId();

                            DtsGoodsSpecification goodsSpecificationChild = new DtsGoodsSpecification();
                            goodsSpecificationChild.setGoodsId(goodsId);
                            //子规格名称
                            goodsSpecificationChild.setSpecification(dto.getBothSpecification().getSpecificationChildName());
                            goodsSpecificationChild.setValue(childSpecification.getSpecificationValue());
                            goodsSpecificationChild.setAddTime(LocalDateTime.now());
                            goodsSpecificationChild.setUpdateTime(LocalDateTime.now());
                            goodsSpecificationChild.setDeleted(false);
                            //父id就是主规格id
                            goodsSpecificationChild.setPid(specificationPid);
                            goodsSpecificationChild.setGoodsPoroductId(childProductId);
                            goodsSpecificationMapper.insertSelective(goodsSpecificationChild);
                        } else {//修改
                            //货品入库
                            DtsGoodsProduct childProduct = new DtsGoodsProduct();
                            childProduct.setId(childOldGoodsSpecification.getGoodsPoroductId());
                            childProduct.setGoodsId(goodsId);
                            childProduct.setNumber(childSpecification.getNumber());
                            childProduct.setUpdateTime(LocalDateTime.now());
                            dtsGoodsProductMapper.updateByPrimaryKeySelective(childProduct);

                            DtsGoodsSpecification goodsSpecificationChild = new DtsGoodsSpecification();
                            goodsSpecificationChild.setId(childOldGoodsSpecification.getId());
                            goodsSpecificationChild.setGoodsId(goodsId);
                            //子规格名称
                            goodsSpecificationChild.setSpecification(dto.getBothSpecification().getSpecificationChildName());
                            goodsSpecificationChild.setValue(childSpecification.getSpecificationValue());
                            goodsSpecificationChild.setUpdateTime(LocalDateTime.now());
                            //父id就是主规格id
                            goodsSpecificationChild.setPid(specificationPid);
                            goodsSpecificationMapper.updateByPrimaryKeySelective(goodsSpecificationChild);
                        }
                    }
                }



                if(null!=specification.getOneLevelExtendScaleVipType1()||null!=specification.getTwoLevelExtendScaleVipType1()){
                    if(null==specification.getOneLevelExtendScaleVipType1()){
                        specification.setOneLevelExtendScaleVipType1(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType1()){
                        specification.setTwoLevelExtendScaleVipType1(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType1());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType1());
                    record.setVipType(VipTypeEnums.TYPE1.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType2()||null!=specification.getTwoLevelExtendScaleVipType2()){
                    if(null==specification.getOneLevelExtendScaleVipType2()){
                        specification.setOneLevelExtendScaleVipType2(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType2()){
                        specification.setTwoLevelExtendScaleVipType2(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType2());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType2());
                    record.setVipType(VipTypeEnums.TYPE2.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType3()||null!=specification.getTwoLevelExtendScaleVipType3()){
                    if(null==specification.getOneLevelExtendScaleVipType3()){
                        specification.setOneLevelExtendScaleVipType3(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType3()){
                        specification.setTwoLevelExtendScaleVipType3(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType3());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType3());
                    record.setVipType(VipTypeEnums.TYPE3.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
                if(null!=specification.getOneLevelExtendScaleVipType4()||null!=specification.getTwoLevelExtendScaleVipType4()){
                    if(null==specification.getOneLevelExtendScaleVipType4()){
                        specification.setOneLevelExtendScaleVipType4(new BigDecimal("-1098"));
                    }
                    if(null==specification.getTwoLevelExtendScaleVipType4()){
                        specification.setTwoLevelExtendScaleVipType4(new BigDecimal("-1098"));
                    }
                    DtsGoodsCommissionConfig record=new DtsGoodsCommissionConfig();
                    record.setGoodsPoroductId(productId);
                    record.setAddTime(LocalDateTime.now());
                    record.setGoodsOneLevelExtendScale(specification.getOneLevelExtendScaleVipType4());
                    record.setGoodsTwoLevelExtendScale(specification.getTwoLevelExtendScaleVipType4());
                    record.setVipType(VipTypeEnums.TYPE4.getId());
                    goodsCommissionConfigMapper.insertSelective(record);
                }
            }

        }
        lock.unlock();
        return ResponseUtil.ok("修改成功", null);
    }

    /**
     * 删除规格
     *
     * @param specificationId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object delSpecification(Integer specificationId) {
        RLock lock = redisson.getLock("admin_delSpecification_" + specificationId);
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        DtsGoodsSpecification oldGoodsSpecification = goodsSpecificationMapper.selectByPrimaryKey(specificationId);
        if (null == oldGoodsSpecification) {
            return ResponseUtil.badArgument("规格不存在");
        }
        oldGoodsSpecification.setDeleted(true);
        oldGoodsSpecification.setUpdateTime(LocalDateTime.now());
        goodsSpecificationMapper.updateByPrimaryKeySelective(oldGoodsSpecification);

        if (oldGoodsSpecification.getPid().intValue() == 0) {//如果是主规格
            //删除货品
            DtsGoodsProduct goodsProduct = new DtsGoodsProduct();
            goodsProduct.setId(oldGoodsSpecification.getGoodsPoroductId());
            goodsProduct.setDeleted(true);
            goodsProduct.setUpdateTime(LocalDateTime.now());
            dtsGoodsProductMapper.updateByPrimaryKeySelective(goodsProduct);
            if (commonMapperEx.isHaveChildSpecification(oldGoodsSpecification.getId())) { //判断下面是否有子规格
                //删除下面的子规格
                DtsGoodsSpecification childSpecification = new DtsGoodsSpecification();
                childSpecification.setDeleted(true);
                childSpecification.setUpdateTime(LocalDateTime.now());
                DtsGoodsSpecificationExample childExample = new DtsGoodsSpecificationExample();
                childExample.createCriteria().andPidEqualTo(oldGoodsSpecification.getId());
                goodsSpecificationMapper.updateByExampleSelective(childSpecification, childExample);
                List<DtsGoodsSpecification>  childSpecificationList=   goodsSpecificationMapper.selectByExample(childExample);
                childSpecificationList.forEach(item->{
                    //删除子规格下面的货品
                    DtsGoodsProduct childGoodsProduct = new DtsGoodsProduct();
                    childGoodsProduct.setId(item.getGoodsPoroductId());
                    childGoodsProduct.setDeleted(true);
                    childGoodsProduct.setUpdateTime(LocalDateTime.now());
                    dtsGoodsProductMapper.updateByPrimaryKeySelective(childGoodsProduct);
                });

            }
        }else{//子规格，删除下面的货品
            DtsGoodsProduct childGoodsProduct = new DtsGoodsProduct();
            childGoodsProduct.setId(oldGoodsSpecification.getGoodsPoroductId());
            childGoodsProduct.setDeleted(true);
            childGoodsProduct.setUpdateTime(LocalDateTime.now());
            dtsGoodsProductMapper.updateByPrimaryKeySelective(childGoodsProduct);
        }
        lock.unlock();
        return ResponseUtil.ok("删除成功", null);
    }


}
